package com.manager.common.job;

import com.jcraft.jsch.ChannelSftp;
import com.manager.common.constant.MimicryTcpFileType;
import com.manager.common.ssh.RemoteExecuteLinuxCommandTools;
import com.manager.common.utils.DataTools;
import com.manager.common.utils.LargeFileHttpUploadUtils;
import com.manager.common.vo.ResultVO;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * @ClassName IntervalGetStateJsonJob
 * @Description TODO
 * @Author @LiuHongJun
 * @Date 2025/9/12 13:44
 * @Version 1.0
 */
//@Component
public class IntervalGetStateJsonJob {

    private int fileNum = 0;

    @Scheduled(fixedDelay = 3 * 1000)
    public void execute() throws Exception{
       try{
           if(fileNum > 4){
               fileNum = 0;
           }
           String mustNewFilePath = "/opt/data/stats_"+fileNum+".json";
           System.out.println(mustNewFilePath);
           List<String> out = new ArrayList<>();
           String localFilePath = LargeFileHttpUploadUtils.getUploadFileSavePath() + "/" + System.currentTimeMillis() + "_" + 6 + ".json";
           boolean flag = RemoteExecuteLinuxCommandTools.downloadFileBySftp(mustNewFilePath,localFilePath,out);

           if(flag){
               String jsonData = FileUtils.readFileToString(new File(localFilePath), "utf-8");
               if(StringUtils.isEmpty(jsonData)){
                   return ;
               }
               JSONObject jsonObject = JSONObject.fromObject(jsonData);
               jsonObject.put("network_sate",1);
               DataTools.putCacheData(MimicryTcpFileType.STATE_COUNT.getEnDesc(), jsonObject);
           }
           fileNum++;
       }catch (Exception e){
           JSONObject jsonObject = DataTools.getCacheData(MimicryTcpFileType.STATE_COUNT.getEnDesc());
           if(jsonObject != null){
               jsonObject.put("network_sate",0);
               DataTools.putCacheData(MimicryTcpFileType.STATE_COUNT.getEnDesc(), jsonObject);
           }
       }
     /*   String filePath = "D:\\lhj\\upload\\uploadFiles\\2025-09-16\\1757984954078_6.json";
        String jsonData = FileUtils.readFileToString(new File(filePath), "utf-8");
        if(StringUtils.isEmpty(jsonData)){
            return ;
        }
        JSONObject jsonObject = JSONObject.fromObject(jsonData);
        jsonObject.put("network_sate",1);
        DataTools.putStateCacheData(jsonObject);*/
    }
}


